home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / list.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  9.2 KB  |  359 lines

  1. /*
  2.  * @(#)List.java    1.18 95/09/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.util.Vector;
  22. import java.awt.peer.ListPeer;
  23.  
  24. /**
  25.  * A scrolling list of text items.
  26.  *
  27.  * @version     1.18, 09/01/95
  28.  * @author     Sami Shaio
  29.  */
  30. public class List extends Component {
  31.     Vector    items = new Vector();
  32.     int        rows = 0;
  33.     boolean    multipleSelections = false;
  34.     int        selected[] = new int[0];
  35.     int        visibleIndex = -1;
  36.  
  37.     /**
  38.      * Creates a new scrolling list initialized with no visible Lines
  39.      * or multiple selections.
  40.      */
  41.     public List() {
  42.     this(0, false);
  43.     }
  44.  
  45.     /**
  46.      * Creates a new scrolling list initialized with the specified 
  47.      * number of visible lines and a boolean stating whether multiple
  48.      * selections are allowed or not.
  49.      * @param rows the number of items to show.
  50.      * @param multipleSelections if true then multiple selections are allowed.
  51.      */
  52.     public List(int rows, boolean multipleSelections) {
  53.     this.rows = rows;
  54.     this.multipleSelections = multipleSelections;
  55.     }
  56.  
  57.     /**
  58.      * Creates the peer for the list.  The peer allows us to modify the
  59.      * list's appearance without changing its functionality.
  60.      */
  61.     public synchronized void addNotify() {
  62.     peer = getToolkit().createList(this);
  63.     super.addNotify();
  64.     visibleIndex = -1;
  65.     }
  66.  
  67.     /**
  68.      * Removes the peer for this list.  The peer allows us to modify the
  69.      * list's appearance without changing its functionality.
  70.      */
  71.     public synchronized void removeNotify() {
  72.     ListPeer peer = (ListPeer)this.peer;
  73.     selected = peer.getSelectedIndexes();
  74.     super.removeNotify();
  75.     }
  76.     
  77.     /**
  78.      * Returns the number of items in the list.
  79.      * @see #getItem
  80.      */
  81.     public int countItems() {
  82.     return items.size();
  83.     }
  84.  
  85.     /**
  86.      * Gets the item associated with the specified index.
  87.      * @param index the position of the item
  88.      * @see #countItems
  89.      */
  90.     public String getItem(int index) {
  91.     return (String)items.elementAt(index);
  92.     }
  93.  
  94.     /**
  95.      * Adds the specified item to the end of scrolling list.
  96.      * @param item the item to be added
  97.      */
  98.     public synchronized void addItem(String item) {
  99.     items.addElement(item);
  100.     ListPeer peer = (ListPeer)this.peer;
  101.     if (peer != null) {
  102.         peer.addItem(item);
  103.     }
  104.     }
  105.  
  106.     /**
  107.      * Clears the list.
  108.      * @see #delItem
  109.      * @see #delItems
  110.      */
  111.     public synchronized void clear() {
  112.     if (peer != null) {
  113.         ((ListPeer)peer).clear();
  114.     }
  115.     items = new Vector();
  116.     selected = new int[0];
  117.     }
  118.  
  119.     /**
  120.      * Delete an item from the list.
  121.      */
  122.     public synchronized void delItem(int position) {
  123.     delItems(position, position);
  124.     }
  125.  
  126.     /**
  127.      * Delete multiple items from the list.
  128.      */
  129.     public synchronized void delItems(int start, int end) {
  130.     for (int i=end; i >= start; i--) {
  131.         items.removeElementAt(i);
  132.     }
  133.     if (peer != null) {
  134.         ((ListPeer)peer).delItems(start, end);
  135.     }
  136.     }
  137.  
  138.     /**
  139.      * Get the selected item on the list or -1 if no item is selected.
  140.      * @see #select
  141.      * @see #deselect
  142.      * @see #isSelected
  143.      */
  144.     public synchronized int getSelectedIndex() {
  145.     int sel[] = getSelectedIndexes();
  146.     return (sel.length == 1) ? sel[0] : -1;
  147.     }
  148.  
  149.     /**
  150.      * Returns the selected indexes on the list.
  151.      * @see #select
  152.      * @see #deselect
  153.      * @see #isSelected
  154.      */
  155.     public synchronized int[] getSelectedIndexes() {
  156.     ListPeer peer = (ListPeer)this.peer;
  157.     if (peer != null) {
  158.         selected = peer.getSelectedIndexes();
  159.     }
  160.     return selected;
  161.     }
  162.  
  163.     /**
  164.      * Returns the selected item on the list or null if no item is selected.
  165.      * @see #select
  166.      * @see #deselect
  167.      * @see #isSelected
  168.      */
  169.     public synchronized String getSelectedItem() {
  170.     int index = getSelectedIndex();
  171.     return (index < 0) ? null : getItem(index);
  172.     }
  173.  
  174.     /**
  175.      * Returns the selected items on the list.
  176.      * @see #select
  177.      * @see #deselect
  178.      * @see #isSelected
  179.      */
  180.     public synchronized String[] getSelectedItems() {
  181.     int sel[] = getSelectedIndexes();
  182.     String str[] = new String[sel.length];
  183.     for (int i = 0 ; i < sel.length ; i++) {
  184.         str[i] = getItem(sel[i]);
  185.     }
  186.     return str;
  187.     }
  188.  
  189.     /**
  190.      * Selects the item at the specified index.
  191.      * @param index the position of the item to select
  192.      * @see #getSelectedItem
  193.      * @see #deselect
  194.      * @see #isSelected
  195.      */
  196.     public synchronized void select(int index) {
  197.     ListPeer peer = (ListPeer)this.peer;
  198.     if (peer != null) {
  199.         peer.select(index);
  200.         return;
  201.     }
  202.  
  203.     for (int i = 0 ; i < selected.length ; i++) {
  204.         if (selected[i] == index) {
  205.         return;
  206.         }
  207.     }
  208.     if (!multipleSelections) {
  209.         selected = new int[1];
  210.         selected[0] = index;
  211.     } else {
  212.         int newsel[] = new int[selected.length + 1];
  213.         System.arraycopy(selected, 0, newsel, 0, selected.length);
  214.         newsel[selected.length] = index;
  215.         selected = newsel;
  216.     }
  217.     }
  218.  
  219.     /**
  220.      * Deselects the item at the specified index.
  221.      * @param index the position of the item to deselect
  222.      * @see #select
  223.      * @see #getSelectedItem
  224.      * @see #isSelected
  225.      */
  226.     public synchronized void deselect(int index) {
  227.     ListPeer peer = (ListPeer)this.peer;
  228.     if (peer != null) {
  229.         peer.deselect(index);
  230.     }
  231.  
  232.     for (int i = 0 ; i < selected.length ; i++) {
  233.         if (selected[i] == index) {
  234.         int newsel[] = new int[selected.length - 1];
  235.         System.arraycopy(selected, 0, newsel, 0, i);
  236.         System.arraycopy(selected, i+1, newsel, i, selected.length - (i+1));
  237.         selected = newsel;
  238.         return;
  239.         }
  240.     }
  241.     }
  242.  
  243.     /**
  244.      * Returns true if the item at the specified index has been selected;
  245.      * false otherwise.
  246.      * @param index the item to be checked
  247.      * @see #select
  248.      * @see #deselect
  249.      * @see #isSelected
  250.      */
  251.     public synchronized boolean isSelected(int index) {
  252.     int sel[] = getSelectedIndexes();
  253.     for (int i = 0 ; i < sel.length ; i++) {
  254.         if (sel[i] == index) {
  255.         return true;
  256.         }
  257.     }
  258.     return false;
  259.     }
  260.  
  261.     /**
  262.      * Returns the number of visible lines in this list.
  263.      */
  264.     public int getRows() {
  265.     return rows;
  266.     }
  267.  
  268.     /**
  269.      * Returns true if this list allows multiple selections.
  270.      * @see #setMultipleSelections
  271.      */
  272.     public boolean allowsMultipleSelections() {
  273.     return multipleSelections;
  274.     }
  275.  
  276.     /**
  277.      * Sets whether this list should allow multiple selections or not.
  278.      * @param v the boolean to allow multiple selections
  279.      * @see #allowsMultipleSelections
  280.      */
  281.     public void setMultipleSelections(boolean v) {
  282.     if (v != multipleSelections) {
  283.         multipleSelections = v;
  284.         ListPeer peer = (ListPeer)this.peer;
  285.         if (peer != null) {
  286.         peer.setMultipleSelections(v);
  287.         }
  288.     }
  289.     }
  290.  
  291.     /**
  292.      * Gets the index of the item that was last made visible by the method
  293.      * makeVisible.
  294.      */
  295.     public int getVisibleIndex() {
  296.     return visibleIndex;
  297.     }
  298.  
  299.     /**
  300.      * Forces the item at the specified index to be visible.
  301.      * @param index the position of the item
  302.      * @see #getVisibleItem
  303.      */
  304.     public void makeVisible(int index) {
  305.     ListPeer peer = (ListPeer)this.peer;
  306.     if (peer != null) {
  307.         peer.makeVisible(index);
  308.     } else {
  309.         visibleIndex = index;
  310.     }
  311.     }
  312.  
  313.     /**
  314.      * Returns the preferred dimensions needed for the list with the specified
  315.      * amount of rows.
  316.      * @param rows amount of rows in list.
  317.      */
  318.     public Dimension preferredSize(int rows) {
  319.     ListPeer peer = (ListPeer)this.peer;
  320.     return (peer != null) ? peer.preferredSize(rows) : super.preferredSize();
  321.     }
  322.  
  323.     /**
  324.      * Returns the preferred dimensions needed for the list.
  325.      * @return the preferred size with the specified number of rows if the 
  326.      * row size is greater than 0. 
  327.      * 
  328.      */
  329.     public Dimension preferredSize() {
  330.     return (rows > 0) ? preferredSize(rows) : super.preferredSize();
  331.     }
  332.  
  333.     /**
  334.      * Returns the minimum dimensions needed for the amount of rows in the 
  335.      * list.
  336.      * @param rows minimum amount of rows in the list
  337.      */
  338.     public Dimension minimumSize(int rows) {
  339.     ListPeer peer = (ListPeer)this.peer;
  340.     return (peer != null) ? peer.minimumSize(rows) : super.minimumSize();
  341.     }
  342.  
  343.     /**
  344.      * Returns the minimum dimensions needed for the list.
  345.      * @return the preferred size with the specified number of rows if
  346.      * the row size is greater than zero.
  347.      */
  348.     public Dimension minimumSize() {
  349.     return (rows > 0) ? minimumSize(rows) : super.minimumSize();
  350.     }
  351.  
  352.     /**
  353.      * Returns the parameter String of this list. 
  354.      */
  355.     protected String paramString() {
  356.     return super.paramString() + ",selected=" + getSelectedItem();
  357.     }
  358. }
  359.